﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LibrarySystem.Models;
using LibrarySystem.LibraryUtils.DAL.DAOs;
using System.Web.Security;

namespace LibrarySystem.Controllers.LibraryControllers
{
    public class SkippingController : Controller
    {
        [HttpPost]
        public JsonResult GetSkippingList(int jtStartIndex = 0, int jtPageSize = 0, string jtSorting = null,
            string borrowingID = null, int employerID = 0)
        {
            try
            {
                Library_Skippings libSkip = new Library_Skippings();
                List<SkippingModel> skipList = libSkip.GetDataList(jtStartIndex, jtPageSize, jtSorting, borrowingID, employerID);
                return Json(new { Result = "OK", Records = skipList, TotalRecordCount = skipList.Count });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = ex.Message });
            }
        }

        [HttpPost]
        public JsonResult AddSkipping(SkippingModel model)
        {
            try
            {
                Library_Borrowings libBro = new Library_Borrowings();
                List<BorrowingModel> list = libBro.GetDataList(0,0,null,null,0,-1,0, model.BorrowingID);
                if (list.Count == 0)
                {
                    return Json(new { Result = "Error", Message = "Mã mượn sách không tồn tại. Mời nhập lại!" });
                }
                
                if (!ModelState.IsValid)
                {
                    return Json(new { Result = "Error", Message = "The data is invalid. Please try again!" });
                }
                Library_Skippings libSkip = new Library_Skippings();
                MembershipUser currentUser = Membership.GetUser(User.Identity.Name, true /* userIsOnline */);
                //
                int employeeID = libSkip.GetEmployeeIdByUsername(currentUser.UserName);
                model.EmployerID = employeeID;
                var add = libSkip.AddNewData(model);
                return Json(new { Result = "OK", Record = add });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "Error", Message = ex.Message });
            }
        }

        [HttpPost]
        public JsonResult UpdateSkipping(SkippingModel model)
        {
            try
            {
                Library_Skippings libSkip = new Library_Skippings();
                if (!ModelState.IsValid)
                {
                    return Json(new { Result = "Error", Message = "The data is invalid. Please try again!" });
                }
                libSkip.UpdateData(model);
                return Json(new { Result = "OK" });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "Error", Message = ex.Message });
            }
        }

        [HttpPost]
        public JsonResult DeleteSkipping(int Id)
        {
            try
            {
                Library_Skippings libSkip = new Library_Skippings();
                if (!ModelState.IsValid)
                {
                    return Json(new { Result = "Error", Message = "The data is invalid. Please try again!" });
                }
                libSkip.DeleteData(Id);
                return Json(new { Result = "OK" });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "Error", Message = ex.Message });
            }
        }
    }
}
